package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.*;
import dao.CompanyDAO;
import dao.DataPermissionDAO;
import entity.Company;
import entity.DataPermission;

@WebServlet("/data_permission")
public class  DataPermissionServlet extends HttpServlet {
	private DataPermissionDAO permissiondao = new DataPermissionDAO();
	private String action;
	public DataPermissionServlet() {
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		this.action=request.getParameter("userno");
		Permission_add(request);
		request.getRequestDispatcher("/UserList.jsp").forward(request, response);
	}

	private void Permission_add(HttpServletRequest request) {
		try {
			DataPermission fp=new DataPermission();
			fp.setUserno(this.action);
			//String userno="1";
			//fp.setUserno(userno);
			String[] uids =request.getParameterValues("x");
			CompanyDAO contentsdao = new CompanyDAO();
			  List<Company> list =contentsdao.loadAllCompany();
				
			  if(list!=null&&list.size()>0){
			for(int i=0;i<list.size();i++)
			{
				Company ex= list.get(i);
				int datakey=0;
			    for(int j=0;j<request.getParameterValues("x").length;j++)
			    {
				
				char [] stringArr = uids[j].toCharArray();
				if(uids[j].equals(list.get(i).getCcompany())){
					datakey=1;
					
				}
				else ;
				fp.setComno(list.get(i).getComno());
				fp.setDatakey(datakey);
				this.permissiondao.permission_edit(fp);
			    }
			}
			}
			
	        
		 } catch (Exception ex) {
			 
	         request.setAttribute("errormsg", ex.getMessage());
	         
	     }
		
	}
}
